var cgdtz = {
    initQuery: function () {
        //采购单位加载
        $('#cgdtz_cgdtzw').combotree({
            loadFilter: function (rows) {
                return convert(rows);
            },
            onSelect: function (record) {
                $('#cgdtz_cgr').combobox({
                    url: $.webPath() + '/sys/yg/ygXmList?zgjgId=' + record.id,
                    valueField: 'id',
                    textField: 'xm'
                });
            }
        });
    },
    initDatagrid: function () {
        $("#grid_cgdtz").datagrid(cgdtz.dataOptions);
    },
    initBtn: function () {
        //调整
        $("#cgdtz-edit").click(function () {
            var row = $('#grid_cgdtz').datagrid('getSelected');
            var lx = row.cglx == "采购入库" ? "cgrk" : "tpcght";
            cgdtz.showWindow({"cgd.id": row.id, "htType": lx});
        });
        //申请
        $("#cgdtz-apply").click(function () {
            var row = $('#grid_cgdtz').datagrid('getSelected');
            if (row) {
                $.messager.confirm('确认', '是否确认申请冲销此采购单', function (r) {
                    if (r) {
                        $.post($.webPath() + '/cggl/cgdgl/cgdtzApply', {'cgd.id': row.id}, function (data) {
                            var obj = eval('(' + data + ')');
                            if (obj.statusCode == '200') {
                                $('#grid_cgdtz').datagrid('reload');
                            } else {
                                $.messager.alert('提示', obj.message, 'info');
                            }
                        });
                    }
                });
            } else {
                $.messager.alert('提示', "请选择要冲销的采购单", 'info');
            }
        });
        //取消申请
        $("#cgdtz-cancel").click(function () {
            var row = $('#grid_cgdtz').datagrid('getSelected');
            if (row) {
                $.messager.confirm('确认', '是否确认取消申请冲销此采购单', function (r) {
                    if (r) {
                        $.post($.webPath() + '/cggl/cgdgl/cgdtzCancel', {'cgd.id': row.id}, function (data) {
                            var obj = eval('(' + data + ')');
                            if (obj.statusCode == '200') {
                                $('#grid_cgdtz').datagrid('reload');
                            } else {
                                $.messager.alert('提示', obj.message, 'info');
                            }
                        });
                    }
                });
            } else {
                $.messager.alert('提示', "请选择要取消冲销的采购单", 'info');
            }
        });
        //审批
        $("#cgdtz-examine").click(function () {
            var row = $('#grid_cgdtz').datagrid('getSelected');
            if (row) {
                $.messager.confirm('确认', '是否确认审批冲销此采购单', function (r) {
                    if (r) {
                        $.post($.webPath() + '/cggl/cgdgl/cgdtzExamine', {'cgd.id': row.id}, function (data) {
                            var obj = eval('(' + data + ')');
                            if (obj.statusCode == '200') {
                                $('#grid_cgdtz').datagrid('reload');
                            } else {
                                $.messager.alert('提示', obj.message, 'info');
                            }
                        });
                    }
                });
            } else {
                $.messager.alert('提示', "请选择要审批的采购单", 'info');
            }
        });
        //查询
        $("#btn_cgdtz_search").click(function () {
            $('#grid_cgdtz').datagrid({queryParams: cgdtz.getQuery()});
        });
    },
    setBtnStatus: function () {
        var row = $("#grid_cgdtz").datagrid("getSelected");
        if (row && row.cglx == 'zddh') {
            $("#cgdtz-apply").hide();
            $("#cgdtz-cancel").hide();
            $("#cgdtz-examine").hide();
            if (row && row.sftzd == 0) {
                $("#cgdtz-edit").css('display', 'block');
            } else {
                $("#cgdtz-edit").css('display', 'none');
            }
        } else {
            if (row && (row.tzsqzt == 'wsq' || !row.tzsqzt)) {
                $("#cgdtz-apply").show();
                $("#cgdtz-cancel").hide();
                $("#cgdtz-examine").hide();
                $("#cgdtz-edit").hide();
            } else if (row && row.tzsqzt == 'ysq') {
                $("#cgdtz-apply").hide();
                $("#cgdtz-cancel").show();
                $("#cgdtz-examine").show();
                $("#cgdtz-edit").hide();
            } else {
                $("#cgdtz-apply").hide();
                $("#cgdtz-cancel").hide();
                $("#cgdtz-examine").hide();
                if (row && row.sftzd == 0) {
                    $("#cgdtz-edit").css('display', 'block');
                } else {
                    $("#cgdtz-edit").css('display', 'none');
                }
            }
        }
    },
    dataOptions: {
        url: $.webPath() + "/cggl/cgdgl/cgdtzlist",
        pageNumber: 1,
        onDblClickCell: function (index, field, value) {
            if (field == "cgdtzh") {
                $("#cgdtz-edit:visible").click();
            }
        },
        loadFilter: function (data) {
            $.each(data.rows, function () {
                this.tzsqzt_text = this.tzsqzt == 'wsq' ? "未申请" : this.tzsqzt == 'ysq' ? "已申请" : this.tzsqzt == 'ysp' ? "已审批" : "未申请";
                this.cglx_text = this.cglx == "cgrk" ? "采购入库" : this.cglx == "zddh" ? "直达到货" : this.cglx == "bght" ? "保供采购"  : this.cglx == "nbcg" ? "内部采购" : "统配采购";
            })
            return data;
        },
        rowStyler: function (index, row) {
            if (row.sftzd == 1) {
                return 'background-color:rgba(255, 130, 87, 0.64);';
            }
        },
        onLoadSuccess: function () {
            $(this).datagrid('selectRow', 0);
            cgdtz.setBtnStatus();

            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        onClickRow: function () {
            cgdtz.setBtnStatus();
        },
        onSelect: function (rowIndex, rowData) {
            if (rowData) {
                cgdtz.setBtnStatus();
            }
        },
        columns: [
            [
                {field: 'cgdh', title: '送货单号', align: 'center', width: 112, formatter: cgdtzCell},
                {field: 'fhrq', title: '发货日期', align: 'center', width: 77},
                {field: 'cgsl', title: '采购数量', align: 'right',
                    formatter: function (value) {
                        var sl = formatSlForHtml(value);
                        return sl;
                    }, width: 145},
                {field: 'cgje', title: '采购总金额(元)', align: 'right', width: 155,
                    formatter: function (value) {
                        var val = formatJeForHtml2(value);
                        return val;
                    }},
                {field: 'gys_qc', title: '供应商名称', align: 'center', width: 132},
                {field: 'cglx_text', title: '采购类型', align: 'center', width: 63},
                {field: 'cggs_jc', title: '采购公司', align: 'center', width: 80},
                {field: 'sbgs_jc', title: '申报公司', align: 'center', width: 80},
                {field: 'sbbm_qc', title: '申报部门', align: 'center', width: 90},
                {field: 'sbr_xm', title: '申报人', align: 'center', width: 45},
                {field: 'cgsj', title: '制单日期', align: 'center', width: 71},
                {field: 'tzsqzt_text', title: '调整申请状态', align: 'center', width: 49}
            ]
        ]
    },
    showWindow: function (cgdtzdata) {
        var tit = cgdtzdata["cgd.id"] ? "冲销送货单" : "添加送货单";
        cgdtz.win_cgdtz = $.window({
            title: tit,
            iconCls: 'icon-add2',
            width: 1024,
            height: 500,
            url: $.webPath() + "/cggl/cgdtz/cgdtz_content",
            data: cgdtzdata,
            buttons: [
                {
                    text: '保存',
                    handler: cgdtz_edit
                }
            ]
        });
    },
    win: {},
    showDetail: function (id) {
        if ($("#layout_cgdtz #cgdtz_id").val() != id) {
            cgdtz.alwayshowDetail(id);
        }
    },
    alwayshowDetail: function (id) {
        $('#layout_cgdtz').layout('remove', 'center');
        $('#layout_cgdtz').layout('add', {
            region: 'center',
            split: true,
            href: $.webPath() + '/cggl/cgdtz/cgdtz_detail?cgd.id=' + id
        });
    },
    getQuery: function () {
        return {
            "zt": $("[name='querycgdtz.zt']").val(),
            "cglx": $("[name='querycgdtz.cglx']").val(),
            "cgdwId": $("#cgdtz_cgdtzw").combotree('getValue'),
            "cgr": $("[name='querycgdtz.cgr']").val(),
            "fhrqStart": $("#querycgdtz_start").datebox('getValue'),
            "fhrqEnd": $("#querycgdtz_end").datebox('getValue'),
            "cgdh": $("[name='querycgdtz.cgdtzh']").val()
        }
    }
}

$(function () {
    //查询条件-加载
    cgdtz.initQuery();
    //按钮-加载
    cgdtz.initBtn();
    //列表-加载
    cgdtz.initDatagrid();
})


function cgdtz_edit() {
    cgdtz.win_cgdtz.mask();
    $("#cgdtz_mx").edatagrid('saveRow');
    $('#cgdtzForm').tabs('select', "送货单");
    $("#cgdtzForm").form('submit', {
        url: $.webPath() + "/cggl/cgdgl/edit_cgdtz",
        onSubmit: function (param) {
            var data = $('#cgdtz_mx').edatagrid("getData");
            if (!data.rows.length) {
                $('#cgdtzForm').tabs('select', "送货明细");
                $.messager.alert("提示", "送货明细需要填写!", "waring");
                cgdtz.win_cgdtz.mask("hide");
                return false;
            }
            var arrayPrefix = 'cgd.cgdmxList';
            //物资计划明细
            var paramString = "{";
            //物资计划明细
            for (var i = 0; i < data.rows.length; i++) {
                {
                    var objectString = $.toJSONStringSubPrefix(data.rows[i], arrayPrefix + "[" + i + "].",
                        [
                            ['wzgg.id', 'gg.id'],
                            'cd.id',
                            'jzfs.id',
                            'hsj',
                            'bhsj',
                            'bz',
                            'sbsl',
                            'js'
                        ]);
                    paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                }
            }
            if (data.rows && data.rows.length > 0)
                paramString = paramString.substring(0, paramString.length - 1);
            paramString += "}";
            $.extend(param, $.parseJSON(paramString));
            return $(this).form('validate');
        },
        success: function (data) {
            cgdtz.win_cgdtz.mask("hide");
            var obj = eval('(' + data + ')');
            if (obj.statusCode == '200') {
                $(cgdtz.win_cgdtz).window('close');
                $('#grid_cgdtz').datagrid('reload');
                $('#layout_cgdtz').layout('panel', 'center').panel('refresh');
                $.messager.show({title: '提示框', msg: obj.message, timeout: 3000, showType: 'slide'});
            } else {
                $.messager.alert('提示', obj.message, 'info');
            }
        }
    });
}
